package m.app.action;

import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.store.model.GuestInfo;
import m.store.model.RepositoryInfo;
import m.store.service.RepositoryInfoService;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentParamMeta;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;

@ActionMeta(name = "app/repositoryInfo",title = "app仓库信息")
public class RepositoryInfoAction extends AppAction{
    private RepositoryInfo model;
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "仓库列表",permission = true),
        params = {
            @DocumentParamMeta(name = "page.index",description = "分页开始位置",type = FieldType.INT),
            @DocumentParamMeta(name = "page.num",description = "分页每页数量",type = FieldType.INT),
            @DocumentParamMeta(name = "params[oid@eq]",description = "条件示例，字段和操作符中间用@，值为空不添加",type = FieldType.STRING,length = 50),
            @DocumentParamMeta(name = "params[oid#desc]",description = "排序示例，字段和操作符中间用#，值为空不生效",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage getList() throws Exception {
        ModelQueryList queryList=toQueryList(RepositoryInfo.class,"*","manageUser.realname");
        queryList.addCondition(QueryCondition.eq("store.oid",PowerUtil.getSessionStoreOid()));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "查询仓库信息",permission = true),
        params = {
            @DocumentParamMeta(name = "model.oid",description = "仓库oid",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage getInfo() throws Exception {
        PowerUtil.verifyPower(PowerUtil.Store);//权限判断
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*","manageUser.*");
        }else{
            getService(RepositoryInfoService.class).verifyRepositoryNum();
            model=new RepositoryInfo();
            model.setDefRepository(ObjectUtil.isEmptyOid(PowerUtil.getDefRepository())?RepositoryInfo.Yes:RepositoryInfo.No);
            model.setStore(PowerUtil.getSessionStore(true));
        }
        return JSONMessage.success("查询成功")
                .push("model",model);
    }

    public RepositoryInfo getModel() {
        return model;
    }

    public void setModel(RepositoryInfo model) {
        this.model = model;
    }
}
